#include"processbar.h"

//vision 1
const char*lable="|/-\\";
void process1()
{
    
   // const int n = 0;
   // char buffer[n];   //C99前的标准不支持变长数组
   
    char buffer[MAX];
    memset(buffer, '\0', MAX);    //初始化数组，memset头文件string.h
    int cent=0;
    int n = strlen(lable);
    buffer[0] = Head; 
    while(cent<MAX)
    {
        printf("[%-100s[ %.3f%%][%c]\r",buffer,cent,lable[cent%n]);   
        //向左预留空间，左对齐  %%打印百分位   -r 只回车不换行   [%d%%],cent ——>比率
        buffer[cent++] = Body;  //后置++是后一个为Body-==
        buffer[cent] = Head;
        if(cent==MAX-1) buffer[cent] = Body;
        usleep(50000); //0.1s睡眠一次，100000毫秒
        fflush(stdout); //刷新缓冲区
    }
    printf("\n");
}


char buffer[MAX] ={0};    //循环由download控制，所以把Body的数组定义为全局变量
//vision 2
void process2(double rate)
{
    static int cent=0;
    int n = strlen(lable);
    if(rate<=1.0) buffer[0] = Head;
    printf("[\033[4;32;44m%-100s\033[0m][%.1f%%][%c]\r",buffer,rate,lable[cent%n]);   
    fflush(stdout);
    buffer[(int)rate] = Body;  //后置++是后一个为Body-==
    if((int)rate< MAX-2) buffer[(int)rate+1] = Head;
    if(rate>=100.0) printf("\n");
    cent++;
}
